# -*- coding: utf-8 -*-

import scrapy
from scrapy.loader import ItemLoader
from chinahr.items import JobInfoItem, ComInfoItem
from scrapyluke.processors import *

__author__ = 'yanyan'


class XiaoLianSpider(scrapy.Spider):
    name = "xiaolian"
    allowed_domains = ["job9151.com"]
    start_urls = ["http://www.job9151.com/jobs/jobs-list.php?page=1"]

    def parse(self, response):
        if response.xpath('//div[@class="list-item item1 f-left"]/a/@href'):
            job_urls = response.xpath('//div[@class="list-item item1 f-left"]/a/@href').extract()
            com_urls = response.xpath('//div[@class="list-item item2 f-left"]/a/@href').extract()
            for url in job_urls:
                yield scrapy.Request(url, callback=self.parse_job_info)
            for url in com_urls:
                yield scrapy.Request(url, callback=self.parse_com_info)
            page = re.findall(r'page=(\d+)', response.url)[0]
            next_page = int(page) + 1
            yield scrapy.Request('http://www.job9151.com/jobs/jobs-list.php?page=%d' % next_page, callback=self.parse)

    def parse_job_info(self, response):
        loader = ItemLoader(item=JobInfoItem(),response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('job_name', u'//div[@class="job-top"]/h3[@class="job-name"]/text()')
        loader.add_xpath('job_company', u'//div[@class="job-top"]/div[@class="job-company clearfix"]/a/text()')
        loader.add_xpath('job_update', u'//div[@class="job-top"]/div[@class="job-watch-info clearfix"]/div[@class="add-time f-left"]/text()')
        loader.add_xpath('job_salary', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][text()="工资待遇："]/span/text()')
        loader.add_xpath('job_nature', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "职位性质：")]/text()', re=u'职位性质：(.*)')
        loader.add_xpath('job_reqAge', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "年龄要求：")]/text()', re=u'年龄要求：(.*)')
        loader.add_xpath('job_recruNums', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "招聘人数：")]/text()', re=u'招聘人数：(.*)')
        loader.add_xpath('job_category', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "职位类别：")]/text()', re=u'职位类别：(.*)')
        loader.add_xpath('job_reqSex', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "性别要求：")]/text()', re=u'性别要求：(.*)')
        loader.add_xpath('job_location', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "工作地区：")]/text()', re=u'工作地区：(.*)')
        loader.add_xpath('job_miniEdu', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "学历要求：")]/text()', re=u'学历要求：(.*)')
        loader.add_xpath('job_experience', u'//div[@class="job-info-detail clearfix"]/div[@class="job-id-item f-left"][contains(text(), "工作经验：")]/text()', re=u'工作经验：(.*)')
        loader.add_xpath('job_desc', u'//div[@class="job-info-content clearfix"]//text()', RemoveTagsL(), ReplaceBlank(''), JoinL(''))
        loader.add_xpath('job_desc_loc', u'//div[@class="job-info-content clearfix"]//text()', re=u'工作地点：([\S]+)')
        return loader.load_item()

    def parse_com_info(self, response):
        loader = ItemLoader(item=ComInfoItem(),response=response)
        loader.add_value('url', value=response.url)
        loader.add_xpath('com_name', u'//div[@class="c-name f-left"]/h2/text()', TakeFirstL())
        loader.add_xpath('com_address', u'//div[@class="c-name f-left"]/p/text()')
        loader.add_xpath('com_nature', u'//ul[@class="company-info-list"]/li[contains(text(),"性质：")]/text()', re=u'性质：(.*)')
        loader.add_xpath('com_industry', u'//ul[@class="company-info-list"]/li[contains(text(),"行业：")]/text()', re=u'行业：(.*)')
        loader.add_xpath('com_size', u'//ul[@class="company-info-list"]/li[contains(text(),"规模：")]/text()', re=u'规模：(.*)')
        loader.add_xpath('com_detail', u'//ul[@class="company-info-list"]/li[contains(text(),"地区：")]/text()', re=u'地区：(.*)')
        loader.add_xpath('com_size', u'//ul[@class="company-info-list"]/li[contains(text(),"网址：")]/a/@href')
        loader.add_xpath('com_intro', u'//div[@class="company-info-wrap"]/div/text()', RemoveTagsL(), ReplaceBlank(''), JoinL(''))
        return loader.load_item()





